#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2e4 + 10, M = 2 * N;
int h[N], e[M], w[M], ne[M], idx;
int n, m;

void add(int a, int b, int c)
{
    e[idx] = b;
    w[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}

int dfs(int u, int mid, int fa)
{
    int res = 1;
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (j == fa || w[i] < mid)continue;
        res += dfs(j, mid, u);
    }
    return res;
}
int main()
{
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> m;
        memset(h, -1, sizeof(h));
        idx = 0;
        for (int i = 0; i < n - 1; i++) {
            int a, b, c;
            cin >> a >> b >> c;
            add(a, b, c);
            add(b, a, c);
        }
        int l = 0;
        int r = 1e7;
        while (l < r) {
            int mid = l + r >> 1;
            if (dfs(0, mid, -1) <= m) {
                r = mid;
            }
            else {
                l = mid + 1;
            }
        }
        cout << l << endl;
    }
    return 0;
}